package com.woniuxy.oldmanapi.controller;

import com.alibaba.druid.wall.Violation;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Electricity;
import com.woniuxy.oldmanapi.service.ElectricityService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.yvo.ElectricityVo;
import com.woniuxy.oldmanapi.yvo.OutApplicationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ElectricityController {
    @Autowired
    ElectricityService electricityService;

    @GetMapping("/Ele/list")
    public ResponseResult<PageInfo<ElectricityVo>> initSelectAllByCons(int buildingId, int storeyId, int roomId, int now, int size) {
        ResponseResult<PageInfo<ElectricityVo>> result = new ResponseResult<>();
        try {
            PageInfo<ElectricityVo> pageInfo = electricityService.initSelectAllByCons(buildingId, storeyId, roomId, now, size);
            if (pageInfo.getList() == null) {
                result.setCode(204);
                result.setMsg("无满足条件对象");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @PutMapping("/Ele/modifyStatus/{id}/{status}")
    public ResponseResult<Void> modifyStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            electricityService.modifyStatus(id, status);
            if (status == 0) {
                result.setCode(200);
                result.setMsg("取消成功");
            } else {
                result.setCode(200);
                result.setMsg("确认成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @DeleteMapping("/Ele/deleteEle/{id}")
    public ResponseResult<Void> deleteEle(@PathVariable("id") Integer id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            electricityService.deleteEle(id);
            result.setCode(200);
            result.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @PostMapping("/Ele/addEle")
    public ResponseResult<Void> addEle(@RequestBody Electricity electricity) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            electricityService.addEle(electricity);
            result.setCode(200);
            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @GetMapping("/Ele/selectById/{id}")
    public ResponseResult<ElectricityVo> selectById(@PathVariable("id") Integer id) {
        ResponseResult<ElectricityVo> result = new ResponseResult<>();
        try {
            ElectricityVo electricityVo = electricityService.selectById(id);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(electricityVo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @PutMapping("/Ele/modifyEle")
    public ResponseResult<Void> modifyEle(@RequestBody Electricity electricity) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            electricityService.modifyEle(electricity);
            result.setCode(200);
            result.setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

}
